package com.nf.health.app.activity.global;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.hyphenate.easeui.widget.photoview.EasePhotoView;
import com.hyphenate.easeui.widget.photoview.PhotoViewAttacher;
import com.nf.health.app.R;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;


public class PictureActivity extends BaseActivity{

    public static final String EXTRA_IMG_LIST = "imgUrls";
    /**
     * 当前索引
     */
    public static final String EXTRA_POSITION = "position";
    /**
     * 标记类型：TYPE_LOCAL_IMG, TYPE_NET_IMG
     */
    public static final String EXTRA_TYPE = "type";

    /**
     * 本地图片
     */
    public static final int TYPE_LOCAL_IMG = 0;
    /**
     * 网络图片
     */
    public static final int TYPE_NET_IMG = 1;


    ViewPager viewPager;
    ArrayList<String> imgUrls;
    TextView tvCount;

    int type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContent(R.layout.activity_picture);

        imgUrls = getIntent().getStringArrayListExtra(EXTRA_IMG_LIST);
        int position = getIntent().getIntExtra(EXTRA_POSITION, 0);
        type = getIntent().getIntExtra(EXTRA_TYPE, 0);
        viewPager = ViewUtil.getView(this, R.id.view_pager);
        tvCount = ViewUtil.getView(this, R.id.tv_count);
        viewPager.setAdapter(new SamplePagerAdapter());

        tvCount.setText((position+1)+"/"+imgUrls.size());
        viewPager.setCurrentItem(position);
        
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvCount.setText((position + 1) + "/" + imgUrls.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class SamplePagerAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return imgUrls == null ? 0 : imgUrls.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            EasePhotoView photoView = new EasePhotoView(container.getContext());
//            Bitmap bitmap = ImageLoader.getInstance().loadImageSync(imgUrls.get(position));
//            photoView.setImageBitmap(bitmap);
            if(type == TYPE_LOCAL_IMG){
//                ImageLoader.getInstance().displayImage("file://"+imgUrls.get(position), photoView, MyApplication.optionsPic);
                ImageLoaderUtils.getinstance(PictureActivity.this).getImage(photoView, "file://"+imgUrls.get(position));
            }else if(type  == TYPE_NET_IMG){
            	ImageLoaderUtils.getinstance(PictureActivity.this).getImage(photoView, imgUrls.get(position));
            }
            photoView.setScaleType(ScaleType.CENTER);
            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    finish();
                }
            });

            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
